
<h1><span class="yiyi-st" id="yiyi-12">numpy.loadtxt</span></h1>
        <blockquote>
        <p>原文：<a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html">https://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html</a></p>
        <p>译者：<a href="https://github.com/wizardforcel">飞龙</a> <a href="http://usyiyi.cn/">UsyiyiCN</a></p>
        <p>校对：（虚位以待）</p>
        </blockquote>
    
<dl class="function">
<dt id="numpy.loadtxt"><span class="yiyi-st" id="yiyi-13"> <code class="descclassname">numpy.</code><code class="descname">loadtxt</code><span class="sig-paren">(</span><em>fname</em>, <em>dtype=&lt;type &apos;float&apos;&gt;</em>, <em>comments=&apos;#&apos;</em>, <em>delimiter=None</em>, <em>converters=None</em>, <em>skiprows=0</em>, <em>usecols=None</em>, <em>unpack=False</em>, <em>ndmin=0</em><span class="sig-paren">)</span><a class="reference external" href="http://github.com/numpy/numpy/blob/v1.11.3/numpy/lib/npyio.py#L682-L966"><span class="viewcode-link">[source]</span></a></span></dt>
<dd><p><span class="yiyi-st" id="yiyi-14">从文本文件加载数据。</span></p>
<p><span class="yiyi-st" id="yiyi-15">文本文件中的每一行必须具有相同数量的值。</span></p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name">
<col class="field-body">
<tbody valign="top">
<tr class="field-odd field"><th class="field-name"><span class="yiyi-st" id="yiyi-16">参数：</span></th><td class="field-body"><p class="first"><span class="yiyi-st" id="yiyi-17"><strong>fname</strong>：文件或str</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-18">要读取的文件，文件名或生成器。</span><span class="yiyi-st" id="yiyi-19">如果文件扩展名为<code class="docutils literal"><span class="pre">.gz</span></code>或<code class="docutils literal"><span class="pre">.bz2</span></code>，则文件首先解压缩。</span><span class="yiyi-st" id="yiyi-20">注意，生成器应该返回Python 3k的字节字符串。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-21"><strong>dtype</strong>：数据类型，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-22">数组的数据类型； default：float。</span><span class="yiyi-st" id="yiyi-23">如果这是一个结构化数据类型，结果数组将是一维的，每行将被解释为数组的一个元素。</span><span class="yiyi-st" id="yiyi-24">在这种情况下，使用的列数必须与数据类型中的字段数匹配。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-25"><strong>注释</strong>：str或sequence，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-26">用于指示注释开始的字符或字符列表；默认值：&apos;＃&apos;。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-27"><strong>分隔符</strong>：str，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-28">用于分隔值的字符串。</span><span class="yiyi-st" id="yiyi-29">默认情况下，这是任何空格。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-30"><strong>转换器</strong>：dict，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-31">将列号映射到将该列转换为float的函数的字典。</span><span class="yiyi-st" id="yiyi-32">例如，如果第0列是日期字符串：<code class="docutils literal"><span class="pre">转换器</span> <span class="pre">=</span> <span class="pre">{0：</span> <span class="pre">datestr2num}  t0&gt;。</span></code></span><span class="yiyi-st" id="yiyi-33">转换器还可用于为缺少的数据提供默认值（但请参阅<a class="reference internal" href="numpy.genfromtxt.html#numpy.genfromtxt" title="numpy.genfromtxt"><code class="xref py py-obj docutils literal"><span class="pre">genfromtxt</span></code></a>）：<code class="docutils literal"><span class="pre">转换器</span> <span class="pre">=</span> <span class="pre">{ 3：</span> <span class="pre">lambda</span> <span class="pre">s：</span> <span class="pre">float（s.strip()</span> <span class="pre">或</span> <span class="pre">}</span></code>。</span><span class="yiyi-st" id="yiyi-34">默认值：无。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-35"><strong>skiprows</strong>：int，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-36">跳过第一个<em class="xref py py-obj">skiprows</em>行；默认值：0。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-37"><strong>usecols</strong>：sequence，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-38">要读取哪些列，0是第一列。</span><span class="yiyi-st" id="yiyi-39">例如，<code class="docutils literal"><span class="pre">usecols</span> <span class="pre">=</span> <span class="pre">（1,4,5）</span></code>将提取第2，第5和第6列。</span><span class="yiyi-st" id="yiyi-40">默认值“无”导致读取所有列。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-41"><strong>解包</strong>：bool，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-42">如果为真，则返回的数组被转置，使得可以使用<code class="docutils literal"><span class="pre">x，</span> <span class="pre">y，</span> <span class="pre">z</span> <span class="pre">=  <span class="pre">loadtxt（...）</span></span></code>。</span><span class="yiyi-st" id="yiyi-43">与结构化数据类型一起使用时，将为每个字段返回数组。</span><span class="yiyi-st" id="yiyi-44">默认值为False。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-45"><strong>ndmin</strong>：int，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-46">返回的数组至少具有<em class="xref py py-obj">ndmin</em>维度。</span><span class="yiyi-st" id="yiyi-47">否则将挤压单维轴。</span><span class="yiyi-st" id="yiyi-48">合法值：0（默认值），1或2。</span></p>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-49"><span class="versionmodified">版本1.6.0中的新功能。</span></span></p>
</div>
</div></blockquote>
</td>
</tr>
<tr class="field-even field"><th class="field-name"><span class="yiyi-st" id="yiyi-50">返回：</span></th><td class="field-body"><p class="first"><span class="yiyi-st" id="yiyi-51"><strong>out</strong>：ndarray</span></p>
<blockquote class="last">
<div><p><span class="yiyi-st" id="yiyi-52">从文本文件读取的数据。</span></p>
</div></blockquote>
</td>
</tr>
</tbody>
</table>
<div class="admonition seealso">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-53">也可以看看</span></p>
<p><span class="yiyi-st" id="yiyi-54"><a class="reference internal" href="numpy.load.html#numpy.load" title="numpy.load"><code class="xref py py-obj docutils literal"><span class="pre">load</span></code></a>，<a class="reference internal" href="numpy.fromstring.html#numpy.fromstring" title="numpy.fromstring"><code class="xref py py-obj docutils literal"><span class="pre">fromstring</span></code></a>，<a class="reference internal" href="numpy.fromregex.html#numpy.fromregex" title="numpy.fromregex"><code class="xref py py-obj docutils literal"><span class="pre">fromregex</span></code></a></span></p>
<dl class="last docutils">
<dt><span class="yiyi-st" id="yiyi-55"><a class="reference internal" href="numpy.genfromtxt.html#numpy.genfromtxt" title="numpy.genfromtxt"><code class="xref py py-obj docutils literal"><span class="pre">genfromtxt</span></code></a></span></dt>
<dd><span class="yiyi-st" id="yiyi-56">加载具有如指定处理的缺失值的数据。</span></dd>
<dt><span class="yiyi-st" id="yiyi-57"><a class="reference external" href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.loadmat.html#scipy.io.loadmat" title="(in SciPy v0.18.1)"><code class="xref py py-obj docutils literal"><span class="pre">scipy.io.loadmat</span></code></a></span></dt>
<dd><span class="yiyi-st" id="yiyi-58">读取MATLAB数据文件</span></dd>
</dl>
</div>
<p class="rubric"><span class="yiyi-st" id="yiyi-59">笔记</span></p>
<p><span class="yiyi-st" id="yiyi-60">此功能旨在成为简单格式化文件的快速读取器。</span><span class="yiyi-st" id="yiyi-61"><a class="reference internal" href="numpy.genfromtxt.html#numpy.genfromtxt" title="numpy.genfromtxt"><code class="xref py py-obj docutils literal"><span class="pre">genfromtxt</span></code></a>函数提供更复杂的处理，例如，具有缺失值的行。</span></p>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-62"><span class="versionmodified">版本1.10.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-63">通过Python float.hex方法生成的字符串可以用作浮点数的输入。</span></p>
<p class="rubric"><span class="yiyi-st" id="yiyi-64">例子</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">io</span> <span class="k">import</span> <span class="n">StringIO</span>   <span class="c1"># StringIO behaves like a file object</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="s2">&quot;0 1</span><span class="se">\n</span><span class="s2">2 3&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">loadtxt</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
<span class="go">array([[ 0.,  1.],</span>
<span class="go">       [ 2.,  3.]])</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="s2">&quot;M 21 72</span><span class="se">\n</span><span class="s2">F 35 58&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">loadtxt</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="p">{</span><span class="s1">&apos;names&apos;</span><span class="p">:</span> <span class="p">(</span><span class="s1">&apos;gender&apos;</span><span class="p">,</span> <span class="s1">&apos;age&apos;</span><span class="p">,</span> <span class="s1">&apos;weight&apos;</span><span class="p">),</span>
<span class="gp">... </span>                     <span class="s1">&apos;formats&apos;</span><span class="p">:</span> <span class="p">(</span><span class="s1">&apos;S1&apos;</span><span class="p">,</span> <span class="s1">&apos;i4&apos;</span><span class="p">,</span> <span class="s1">&apos;f4&apos;</span><span class="p">)})</span>
<span class="go">array([(&apos;M&apos;, 21, 72.0), (&apos;F&apos;, 35, 58.0)],</span>
<span class="go">      dtype=[(&apos;gender&apos;, &apos;|S1&apos;), (&apos;age&apos;, &apos;&lt;i4&apos;), (&apos;weight&apos;, &apos;&lt;f4&apos;)])</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="s2">&quot;1,0,2</span><span class="se">\n</span><span class="s2">3,0,4&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">loadtxt</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">delimiter</span><span class="o">=</span><span class="s1">&apos;,&apos;</span><span class="p">,</span> <span class="n">usecols</span><span class="o">=</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="n">unpack</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span>
<span class="go">array([ 1.,  3.])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span>
<span class="go">array([ 2.,  4.])</span>
</pre></div>
</div>
</dd></dl>
